/**
 * 去除字符串中的空格
 * str 需要处理的字符串
 * type 去除字符串空格的类型
 */
export function $wipeSpace(str, type) {
	switch (type) {
		case 'all':
			return str.replace(/\s*/g, "");
			break;
		case 'both':
			return str.replace(/^\s|\s$/g, "");
			break;
		case 'innerLeft':
			return str.replace(/^\s*/, "");
			break;
		case 'innerRight':
			return str.replace(/(\s*$)/g, "");
			break;
	}
}

/**
 * 清除html中的行内样式
 * html 需要处理的数据
 */
export function $cleanStyle(html) {
	let newHtml = '';
	let reg = /style\s*?=\s*?(['"])[\s\S]*?\1/;
	if (html) {
		newHtml = html.replace(reg, '');
	}
	return newHtml;
}

/**
 * 清除字符串中的html标签
 * html 需要处理的数据
 */
export function $cleanHtmlTag(htmlStr) {
	let text = '';
	let reg = /<[^>]+>/g;
	if (htmlStr) {
		text = htmlStr.replace(reg, '');
	}
	return text;
}

/**
 * 截取带HTML样式的字符串，并保留并自动补齐HTML标签
 * html: 将要截取的HTML字符串
 * nlen: 截取后的长度，包含标签之间的空格
 * isByte: 是否按照字节长度截取
 */
export function $substringHtml(html, nlen, isByte = false) {
	const getPlainText = (str) => {
		let text = str.replace(/<[^<^>]*>/g, "");
		let tlen = isByte ? text.replace(/[^\x00-\xff]/g, "**").length : text.length;
		return /^\d+$/.test(nlen) && tlen >= nlen ? text : false;
	}
	const getTagsData = (str) => {
		let index = 0;
		let reg = /<[^<^>]+>/;
		let matchs = new Array();
		while (reg.test(str)) {
			let obj = new Object();
			let len = str.search(/<[^<^>]+>/) + str.match(/<[^<^>]+>/)[0].length;
			obj['index'] = index + str.search(reg);
			obj['string'] = str.match(reg).toString();
			str = str.substr(len);
			index += len;
			matchs.push(obj);
		}
		return matchs;
	}
	const getSliceHtml = (data) => {
		let sliceHtml = '';
		let startTags = [];
		let rgx1 = /<[^<^>^\/]+>/;
		let rgx2 = /<\/[^<^>^\/]+>/;
		let textStr = getPlainText(data);
		let matchArr = getTagsData(data);
		let selfTags = ['hr', 'br', 'img', 'input', 'meta'];
		if (!textStr) return data;
		if (isByte) {
			let i = 0;
			for (let z = 0; z < textStr.length; z++) {
				i += (textStr.charCodeAt(z) > 255) ? 2 : 1;
				if (i >= nlen) {
					sliceHtml = textStr.slice(0, (z + 1)) + '...';
					break;
				}
			}
		} else {
			sliceHtml = textStr.substr(0, nlen) + '...';
		}
		for (let i = 0; i < matchArr.length; i++) {
			if (sliceHtml.length <= matchArr[i].index) {
				matchArr = matchArr.slice(0, i);
				break;
			} else {
				sliceHtml = sliceHtml.substring(0, matchArr[i].index) + matchArr[i].string + sliceHtml.substr(matchArr[i].index);
				if (rgx1.test(matchArr[i].string.replace(/(\/\/)/g, ""))) {
					let name = matchArr[i].string.replace(/[<>]/g, "").split(" ");
					if (name.length > 0) {
						name = name[0];
						!selfTags.inArray(name) && startTags.push(name);
					}
				} else if (rgx2.test(matchArr[i].string)) {
					let name = matchArr[i].string.replace(/[<\/>]/g, "");
					startTags.length > 0 && startTags[startTags.length - 1] === name && startTags.pop();
				}
			}
		}
		if (startTags.length > 0) {
			for (var i = startTags.length - 1; i >= 0; i--) {
				sliceHtml += '</' + startTags[i] + '>';
			}
		}
		return sliceHtml;
	}
	if (typeof html !== 'string') return '';
	html = html.replace(/(^\s*)|(\s*$)/g, '').replace(/[\r\n]/g, '');
	return getSliceHtml(html);
}